package com.jhoncy.myproject.system.service;

import com.jhoncy.myproject.system.entity.SysMenu;
import com.jhoncy.myproject.system.entity.SysRoleMenu;
import com.jhoncy.myproject.system.entity.SysUserRole;
import com.jhoncy.myproject.system.mapper.SysMenuMapper;
import com.jhoncy.myproject.system.mapper.SysRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zhuangqi
 * @@date 2017/3/14.
 */
@Service
public class SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;


    /**
     * 根据userId获取当前用户菜单列表
     * @param userId
     * @return
     */
    public List<SysMenu> listMenuByUserId(String userId) {
        List<SysUserRole> userRoles = sysRoleService.listRoleByUserId(userId);
        List<SysMenu> menus = this.listMenuByUserRoleList(userRoles);
        menus = listMenu2Tree(menus);
        return menus;
    }

    /**
     * 根据角色列表获取菜单列表
     * @param sysUserRoles
     * @return
     */
    private List<SysMenu> listMenuByUserRoleList(List<SysUserRole> sysUserRoles) {
        List<SysMenu> menuList = new ArrayList<>();
        for (SysUserRole u_role : sysUserRoles) {
            menuList.addAll(listMenuByUserRole(u_role));
        }
        return menuList;
    }

    /**
     * 根据角色获取菜单列表
     * @param userRole
     * @return
     */
    private List<SysMenu> listMenuByUserRole(SysUserRole userRole) {
        List<SysMenu> sysMenus = new ArrayList<>();
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleId(userRole.getRoleId());
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.select(sysRoleMenu);
        for (SysRoleMenu roleMenu : sysRoleMenus){
            sysMenus.add(sysMenuMapper.selectByPrimaryKey(roleMenu.getMenuId()));
        }
        return sysMenus;
    }

    /**
     * 无排序菜单列表->树形菜单树
     * @param sysMenus
     * @return
     */
    private List<SysMenu> listMenu2Tree(List<SysMenu> sysMenus) {
        List<SysMenu> menuTree = new ArrayList<>(); //最终树

        Map<String,SysMenu> midStr = new HashMap<>();   //临时整理容器
        for (SysMenu menu : sysMenus){
            midStr.put(menu.getMenuId(),menu);
        }
        /*
         *   遍历 map 中的TreeNode 找到 父亲节点，如果找不到父亲节点，那这个TreeNode就是根节点。
         *   把自己放到父亲节点的 children 列表中
         */
        for (Map.Entry<String,SysMenu> entry : midStr.entrySet()){
            SysMenu menu_parent = midStr.get(entry.getValue().getParentId());
            if (menu_parent != null)
                midStr.get(entry.getValue().getParentId()).getChildren()
                        .add(entry.getValue());
            else
                menuTree.add(entry.getValue());
        }
        menuSort(menuTree);
        return menuTree;
    }

    /**
     * 把菜单列表丢进来排序
     * @param sysMenus
     */
    private void menuSort(List<SysMenu> sysMenus) {
        for (SysMenu menu : sysMenus){
            if (menu.getChildren().size()>0)
                menuSort(menu.getChildren());
        }
        Collections.sort(sysMenus); //排序
    }
}
